﻿using System.Xml;

namespace AutoBuildEntity.Model
{
    /// <summary>
    ///     配置文件
    /// </summary>
    public class EntityXml
    {
        private readonly string _path;
        public const string EntityXmlName = "_entity.xml";
        public EntityXml(string path)
        {
            _path = path;
        }

        public string MySqlConnString { get; private set; }
        public string MsSqlConnString { get; private set; }

        public string EntityTemplate { get; private set; }

        /// <summary>
        ///     读取_entity.xml
        /// </summary>
        /// <returns></returns>
        public EntityXml Load()
        {
            var xml = new XmlDocument();
            xml.Load(_path);

            var autoEntityNode = xml.SelectSingleNode("AutoEntity");
            if (autoEntityNode == null)
                return this;
            var connStringNode = autoEntityNode.SelectSingleNode("ConnString");
            if (connStringNode != null)
            {
                var msSqlNode = connStringNode.SelectSingleNode("MSSQL");
                if (msSqlNode?.Attributes != null)
                {
                    MsSqlConnString = msSqlNode.Attributes["connectionString"]?.Value;
                }
                var mySqlNode = connStringNode.SelectSingleNode("MySQL");
                if (mySqlNode?.Attributes != null)
                {
                    MySqlConnString = mySqlNode.Attributes["connectionString"]?.Value;
                }
            }
            var templatesNodes = autoEntityNode.SelectSingleNode("Template");
            if (templatesNodes != null) EntityTemplate = templatesNodes.InnerText;
            return this;
        }
    }
}